import {
  getActiveQueryBoxTextbox,
  isLexical,
} from "@/plugins/__ui-groups__/elements/query-box/utils";
import { PromptHistoryService } from "@/plugins/prompt-history/indexed-db/service-init.bg-worker";
import { getTextContent } from "@/utils/dom-utils/lexical-utils";

export const handlePromptSave = async (params?: {
  promptString?: string;
  url?: string;
  type?: "hard" | "soft";
}) => {
  let prompt = params?.promptString;

  if (!params?.promptString) {
    const $activeQueryBoxTextbox = getActiveQueryBoxTextbox();

    if (!$activeQueryBoxTextbox[0]) return;

    if (isLexical($activeQueryBoxTextbox[0])) {
      prompt = getTextContent({
        element: $activeQueryBoxTextbox[0],
        omitDecorators: true,
      });
    } else {
      prompt = $activeQueryBoxTextbox[0].value;
    }
  }

  if (prompt == null || prompt.length === 0 || prompt.trim() === "") return;

  await PromptHistoryService.Instance.deduplicateAdd({
    prompt,
  });
};
